package com.rq.app_base;

import android.app.Application;
import android.content.Context;
import android.os.Environment;

import com.base.rq.BuildConfig;
import com.base.rq.R;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class BASE {
  public static int commonLayoutId;
  public static int statusColorId = R.color.white;
  private static Context ctx;
  private static String mSessionId;
  private static int mNetErrorToast;
  private static String ROOT_DIR;
  private static String BASE_DIR;
  public static int PAGE_SIZE_START = 1;

  public static int TYPE = 1;//0-足浴  1-餐饮

  private static int needLockScreenOrientation = 0;//0-自感应 1-竖屏固定 2-横屏固定

  public static int getLockScreenOrientation() {
    return needLockScreenOrientation;
  }

  public static void setLockScreenOrientation(int orientation) {
    BASE.needLockScreenOrientation = orientation;
  }

  public static void setType(int type) {
    TYPE = type;
  }

  public static boolean isZuYu() {
    return TYPE == 0;
  }

  private static String url = "";

  private static Map<String, String> headMap = new HashMap<>();

  public static void setHead(Map<String, String> headers) {
    headMap.clear();
    headMap.putAll(headers);
  }

  public static Map<String, String> getHead() {
    return headMap;
  }

  public static void setBaseUrl(String baseUrl) {
    BASE.url = baseUrl;
  }

  public static String getBaseUrl() {
    return url;
  }

  public static String getDownloadUrl() {
    return url;
  }

  public static Context getCxt() {
    return ctx;
  }

  //没有以 "/" 结尾
  public static String getBaseDir() {
    return BASE_DIR;
  }

  public static void setCommonLayout(int layout, int color) {
    commonLayoutId = layout;
    statusColorId = color;
  }

  public static void setSessionId(String id) {
    mSessionId = id;
  }

  public static String getSessionId() {
    return mSessionId;
  }

  public static String getString(int res) {//字模块方法
    return getCxt().getString(res);
  }

  public static void refuseContext(Context context) {
    ctx = context;
  }

  public static void init(Application context, String baseDirName) {
    boolean sdCardExist = Environment.getExternalStorageState()
                                     .equals(Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
      ROOT_DIR = Environment.getExternalStorageDirectory().getAbsolutePath();
    } else {
      ROOT_DIR = System.getenv("EXTERNAL_STORAGE");
    }
    BASE_DIR = ROOT_DIR + "/" + baseDirName;
    refuseBase();
  }

  public static void refuseBase() {
    File base = new File(BASE_DIR);
    if (!base.exists()) {
      base.mkdirs();
    }
  }

  public static boolean hasSDPermission() {
    return new File(BASE_DIR).exists();
  }


  public static void setNetErrorToast(int res) {
    mNetErrorToast = res;
  }

  public static int getNetErrorToast() {
    return mNetErrorToast;
  }

  /**
   * @return getUseFrom 所在方法的上层调用处
   */
  public static String getUseFrom() {
    StackTraceElement[] stacks = new Exception().getStackTrace();
    if (stacks != null) {
      try {
        String classname = stacks[2].getFileName(); //获取调用者的类名
        String method_name = stacks[2].getMethodName(); //获取调用者的方法名
        int line = stacks[2].getLineNumber(); //获取调用者的方法名
        return classname + "[" + method_name + "]." + line;
      } catch (Exception e) {
      }
    }
    return "";
  }

  public static boolean isDebug() {
    return BuildConfig.DEBUG;
  }

  public static boolean isRQ() {//调试版本，跳过某些判断
    return new File(ROOT_DIR + "/rq").exists();
  }
}
